package com.ak.jddemo.test;

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: ak
 * @date: 2021/4/20 9:43
 * @description:
 */
@Slf4j
public class SeckillService {
    static AtomicInteger integer = new AtomicInteger();

    @SneakyThrows
    public static void begin() {
        long start = System.currentTimeMillis();
        JSONObject headers = new JSONObject();
        //模拟下单状态
        headers.put("Cookie", SeckillService2.COOKIE);

        HttpUrlConnectionUtil.doGet("https://trade.jd.com/shopping/order/getOrderInfo.action", headers);
        headers = new JSONObject();
        initHeaders(headers);

        headers.put("Cookie", SeckillService2.COOKIE);
        //提交订单
        JSONObject finalHeaders = headers;
        ExecutorService service = Executors.newFixedThreadPool(8);
        log.info("模拟下单初始化时间: {}秒", (System.currentTimeMillis() - start) / 1000d);
        Runnable task = getTask(finalHeaders);
        while (Config.status == 0) {
            service.execute(task);
        }
        log.info("抢购结束");
    }

    private static Runnable getTask(JSONObject finalHeaders) {
        long start = System.currentTimeMillis();
        return new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                if (Config.status == 0) {
                    String submitResponse = HttpUrlConnectionUtil.doPost("https://trade.jd.com/shopping/order/submitOrder.action", null, finalHeaders,true);
                    log.info("已下单，单次抢购: {}秒", (System.currentTimeMillis() - start) / 1000d);
                    log.info("抢购结果{}", submitResponse);
                    if (integer.incrementAndGet() > SeckillService2.ERROR_TIMES) {
                        System.exit(0);
                    }
                }
            }
        };
    }

    private static void initHeaders(JSONObject headers) {
        headers.put("Referer", "http://trade.jd.com/shopping/order/getOrderInfo.action");
        headers.put("origin", "https://trade.jd.com");
        headers.put("Content-Type", "application/json");
        headers.put("x-requested-with", "XMLHttpRequest");
        headers.put("upgrade-insecure-requests", "1");
        headers.put("sec-fetch-user", "?1");
    }

    public static JSONObject getSubData() {
        JSONObject subData = new JSONObject();
        subData.put("overseaPurchaseCookies", "");
        subData.put("vendorRemarks", "[]");
        subData.put("submitOrderParam.sopNotPutInvoice", "false");
        subData.put("submitOrderParam.ignorePriceChange", "1");
        subData.put("submitOrderParam.btSupport", "0");
        subData.put("submitOrderParam.isBestCoupon", "1");
        subData.put("submitOrderParam.jxj", "1");
        subData.put("submitOrderParam.trackID", Login.ticket);
        subData.put("submitOrderParam.eid", Config.eid);
        subData.put("submitOrderParam.fp", Config.fp);
        subData.put("submitOrderParam.needCheck", "1");
        return subData;
    }
}
